<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <Popconfirm :title="t('确认从U8C同步吗？')" @confirm="handleU8C()">
          <a-button type="default" v-auth="'bas:sync:edit'">
            <Icon icon="i-simple-line-icons:settings" /> {{ t('从U8C同步') }}
          </a-button>
        </Popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cuscode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsBasVenBasVenList">
  import { unref, watch } from 'vue';
  import { Popconfirm } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basVenDelete, basVenListData } from '/@/api/bas/ven/basVen';
  import { basVenDisable, basVenEnable } from '/@/api/bas/ven/basVen';
  import { basVenClsTreeData } from '/@/api/bas/ven/basVenCls';
  import { companyTreeData } from '/@/api/sys/company';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { SyncDjTypeEnum } from '/@/enums/srmEnum';
  import { syncFromU8C } from '/@/api/bas/sync/sync';
  import InputForm from './form.vue';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });

  const { t } = useI18n('bas.ven.basVen');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('供应商档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('地区分类'),
        field: 'pkAreacl',
        fieldLabel: 'basVenCls.clsName',
        component: 'TreeSelect',
        componentProps: {
          api: basVenClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('客商编码'),
        field: 'cuscode',
        component: 'Input',
      },
      {
        label: t('客商名称'),
        field: 'cusname',
        component: 'Input',
      },
      {
        label: t('客商简称'),
        field: 'shortName',
        component: 'Input',
      },
      {
        label: t('公司名称'),
        field: 'pkCorp',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
        ifShow: false,
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('客商编码'),
      dataIndex: 'cuscode',
      key: 'a.cuscode',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('客商名称'),
      dataIndex: 'cusname',
      key: 'a.cusname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('客商简称'),
      dataIndex: 'shortName',
      key: 'a.short_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('地区分类'),
      dataIndex: 'basVenCls.clsName',
      key: 'a.pk_areacl',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('公司名称'),
      dataIndex: 'company.companyName',
      key: 'a.pk_corp',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      width: 100,
      align: 'center',
      dictType: 'sys_search_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑供应商档案'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:ven:basVen:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用供应商档案'),
        popConfirm: {
          title: t('是否确认停用供应商档案'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'bas:ven:basVen:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用供应商档案'),
        popConfirm: {
          title: t('是否确认启用供应商档案'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'bas:ven:basVen:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除供应商档案'),
        popConfirm: {
          title: t('是否确认删除供应商档案'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'bas:ven:basVen:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: basVenListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        pkAreacl: props.treeCode,
        'basVenCls.clsName': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await basVenDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await basVenEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await basVenDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleU8C() {
    const res = await syncFromU8C({ djType: SyncDjTypeEnum.Vendor });
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
